# 第 0007 题： 有个目录，里面是你自己写过的程序，统计一下你写过多少行代码。包括空行和注释，但是要分别列出来。
# 目录:当前目录
# https://docs.python.org/zh-cn/3.8/library/io.html#io-overview
# https://docs.python.org/zh-cn/3.8/library/functions.html#open
import os

def file_statistics_line(filepath):
    code_line=0
    note_line=0
    space_line=0
    with open(filepath,encoding='utf-8') as f:
        for line in f:
            if not line.isspace():
                if line.lstrip().startswith('#'):
                    note_line=note_line+1
                else:
                    code_line=code_line+1
            else:
                space_line=space_line+1
    return {'code_line':code_line,'note_line':note_line,'space_line':space_line}
    


def dir_statistics_line(dirpath,suffix):
    code_line=0
    note_line=0
    space_line=0
    for file in os.listdir(dirpath):
        file_path=dirpath+os.sep+file
        #判断是否以suffix参数结尾
        if file.endswith(suffix):
            line_dict=file_statistics_line(file_path)
            code_line=code_line+line_dict['code_line']
            note_line=note_line+line_dict['note_line']
            space_line=space_line+line_dict['space_line']

    return {'code_line':code_line,'note_line':note_line,'space_line':space_line}


# dir_statistics_line(os.getcwd())
line_dict=dir_statistics_line(os.getcwd(),'.py')
print('{0}行代码'.format(line_dict['code_line']))
print('{0}行注释'.format(line_dict['note_line']))
print('{0}行空行'.format(line_dict['space_line']))